<?php

/**
 * @version     $Id: AdminsafeUserDetector.php 3 2010-10-08 17:49:35Z obalyuk87@gmail.com $
 * @package     AdminSafe
 * @subpackage  Components
 * @copyright   Copyright (C) 2010 Oleksandr Balyuk (www.boolcast.com)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL v2
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
// No direct access
defined('_JEXEC') or die('Restricted access');

/**
 * Derived from UserDetector class by  Dragan Dinic  *
 * version  $1.1$
 * @author   Dragan Dinic <dragan@dinke.net>
 * package  dinke.net
 * copyright (C) 2008 Dinke.net
 * 
 * Browser Detection class
 * contains common static method for
 * getting browser version and os
 *
 * It supports most popular browsers (IE, FF, Safari, Opera, Chrome ...)
 * As well as some not so popular (lynx etc.)
 * It doesn't recognize bots (like google, yahooo etc)
 *
 * ---
 *
 * August 2010, class modified by Oleksandr Balyuk <obalyuk@boolcast.com>
 *  - PHP 5 structure
 *  - Improved commenting
 *  - made class easier to use
 *  - added getIpAddress method, since it is related to user detection
 */
class AdminsafeUserDetector {

    /**
     * Gets an IP Address of the user
     * 
     * @return stirng
     */
    public static function getIpAddress() {
        return $_SERVER['REMOTE_ADDR'];
    }

    /**
     * Get browsername and version
     *
     * @example $browser = Browser_Detection::get_browser($_SERVER['HTTP_USER_AGENT']);
     * @param string user agent
     * @return string browser name and version or false if unrecognized
     */
    public static function getBrowser($useragent = null) {

        //
        if (!$useragent) {
            $useragent = $_SERVER['HTTP_USER_AGENT'];
        }

        //  check for most popular browsers first
        //  unfortunately that's ie. We also ignore opera and netscape 8
        //  because they sometimes send msie agent
        if (strpos($useragent, "MSIE") !== false && strpos($useragent, "Opera") === false && strpos($useragent, "Netscape") === false) {
            //deal with IE
            $found = preg_match("/MSIE ([0-9]{1}\.[0-9]{1,2})/", $useragent, $mathes);
            if ($found) {
                return "Internet Explorer " . $mathes[1];
            }
        } elseif (strpos($useragent, "Gecko")) {
            //deal with Gecko based
            //if firefox
            $found = preg_match("/Firefox\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Mozilla Firefox " . $mathes[1];
            }

            //if Netscape (based on gecko)
            $found = preg_match("/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Netscape " . $mathes[1];
            }

            //check chrome before safari because chrome agent contains both
            $found = preg_match("/Chrome\/([^\s]+)/", $useragent, $mathes);
            if ($found) {
                return "Google Chrome " . $mathes[1];
            }

            //if Safari (based on gecko)
            $found = preg_match("/Safari\/([0-9]{2,3}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Safari " . $mathes[1];
            }

            //if Galeon (based on gecko)
            $found = preg_match("/Galeon\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Galeon " . $mathes[1];
            }

            //if Konqueror (based on gecko)
            $found = preg_match("/Konqueror\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Konqueror " . $mathes[1];
            }

            //no specific Gecko found
            //return generic Gecko
            return "Gecko based";
        } elseif (strpos($useragent, "Opera") !== false) {
            //deal with Opera
            $found = preg_match("/Opera[\/ ]([0-9]{1}\.[0-9]{1}([0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Opera " . $mathes[1];
            }
        } elseif (strpos($useragent, "Lynx") !== false) {
            //deal with Lynx
            $found = preg_match("/Lynx\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Lynx " . $mathes[1];
            }
        } elseif (strpos($useragent, "Netscape") !== false) {
            //NN8 with IE string
            $found = preg_match("/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $mathes);
            if ($found) {
                return "Netscape " . $mathes[1];
            }
        } else {
            //unrecognized, this should be less than 1% of browsers (not counting bots like google etc)!
            return false;
        }
    }

    /**
     * Get browsername and version
     *
     * @example $os = Browser_Detection::get_os($_SERVER['HTTP_USER_AGENT']);
     * @param string user agent
     * @return string os name and version or false in unrecognized os
     */
    public static function getOs($useragent = null) {
        //
        if (!$useragent) {
            $useragent = $_SERVER['HTTP_USER_AGENT'];
        }

        //
        $useragent = strtolower($useragent);

        //check for (aaargh) most popular first
        //winxp
        if (strpos("$useragent", "windows nt 5.1") !== false) {
            return "Windows XP";
        } elseif (strpos("$useragent", "windows nt 6.0") !== false) {
            return "Windows Vista";
        } elseif (strpos("$useragent", "windows 98") !== false) {
            return "Windows 98";
        } elseif (strpos("$useragent", "windows nt 5.0") !== false) {
            return "Windows 2000";
        } elseif (strpos("$useragent", "windows nt 5.2") !== false) {
            return "Windows 2003 server";
        } elseif (strpos("$useragent", "windows nt 6.0") !== false) {
            return "Windows Vista";
        } elseif (strpos("$useragent", "windows nt") !== false) {
            return "Windows NT";
        } elseif (strpos("$useragent", "win 9x 4.90") !== false && strpos("$useragent", "win me")) {
            return "Windows ME";
        } elseif (strpos("$useragent", "win ce") !== false) {
            return "Windows CE";
        } elseif (strpos("$useragent", "win 9x 4.90") !== false) {
            return "Windows ME";
        } elseif (strpos("$useragent", "iphone") !== false) {
            return "iPhone";
        } elseif (strpos("$useragent", "mac os x") !== false) {
            return "Mac OS X";
        } elseif (strpos("$useragent", "macintosh") !== false) {
            return "Macintosh";
        } elseif (strpos("$useragent", "linux") !== false) {
            return "Linux";
        } elseif (strpos("$useragent", "freebsd") !== false) {
            return "Free BSD";
        } elseif (strpos("$useragent", "symbian") !== false) {
            return "Symbian";
        } else {
            return false;
        }
    }

}
